package com.antipov.odp.client;

import com.antipov.odp.IOHelper;
import com.antipov.odp.pojo.I;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: Andrew Antipov
 * Date: 18.02.14
 * Time: 23:00
 */
public class Client {

	private static final String HOST = "10.60.5.26";
	private static final int PORT = 5252;

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		String dataFileName = sc.next();
		String outputFileName = sc.next();
		process(dataFileName, outputFileName);
	}

	private static void process(String dataFileName, String outputFileName) {
		IReader reader = new IReader(dataFileName);
		Socket socket = null;
		try {
			socket = new Socket(HOST, PORT);
			BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			StreamTokenizer tok = new StreamTokenizer(in);
			PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
			List<I> res = new ArrayList<I>();

			while (reader.hasNext()) {
				I first = reader.next();
				I second = reader.next();
				out.println("Sum");
				out.println(first.toString());
				out.println(second.toString());
				out.flush();
				I curResponse = IOHelper.readNext(tok);
				res.add(curResponse);
			}
			out.println("End");
			out.flush();

			I[] array = new I[res.size()];
			for (int i = 0; i < array.length; i++) {
				array[i] = res.get(i);
			}
			IOHelper.saveData(array, outputFileName);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				socket.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
